package com.duyj.rule.drools;

import com.duyj.rule.drools.unit.ScopeUnit;
import com.duyj.rule.model.datarow.DataRow;
import com.duyj.rule.model.dto.UserDTO;
import org.drools.ruleunit.DataSource;
import org.drools.ruleunit.RuleUnit;
import org.drools.ruleunit.RuleUnitExecutor;
import org.kie.api.KieBase;
import org.kie.api.io.Resource;
import org.kie.api.io.ResourceType;
import org.kie.api.runtime.KieContainer;
import org.kie.api.runtime.KieContainerSessionsPool;
import org.kie.api.runtime.KieSession;
import org.kie.internal.io.ResourceFactory;
import org.kie.internal.utils.KieHelper;
import org.springframework.stereotype.Service;

/**
 * 〈一句话功能简述〉
 *
 * <p>〈功能详细描述〉
 *
 * @author Think
 * @since 版本号
 */
@Service
public class DataRowDroolsService {


    public UserDTO getUserRuleStr(DataRow dataRow) {
        KieHelper helper = new KieHelper();
        helper.addContent(RuleConstant.SET_VALUE_RULE, ResourceType.DRL);
        KieBase build = helper.build();
        KieSession kSession = build.newKieSession();
        UserDTO user = new UserDTO();
        kSession.insert(dataRow);
        kSession.insert(user);
        kSession.fireAllRules();
        kSession.dispose();
        return user;
    }

    private boolean init = false;

    KieContainerSessionsPool pool;

    private void init_52GetSet() {
        if (init) {
            return;
        }
        Resource resource = ResourceFactory.newClassPathResource("rules/user-setValue-52GetSet.drl", "UTF-8");
        KieHelper helper = new KieHelper();
        helper.addResource(resource);
        KieContainer kieContainer = helper.getKieContainer();
        pool = kieContainer.newKieSessionsPool(50);
        init = true;
    }

    private void init_2GetSet() {
        if (init) {
            return;
        }
        Resource resource = ResourceFactory.newClassPathResource("rules/user-setValue-2GetSet.drl", "UTF-8");
        KieHelper helper = new KieHelper();
        helper.addResource(resource);
        KieContainer kieContainer = helper.getKieContainer();
        pool = kieContainer.newKieSessionsPool(50);
        init = true;
    }


    public UserDTO getUserRuleFile(DataRow dataRow, DataRow dataRow2) {
        Resource resource = ResourceFactory.newClassPathResource("rules/user-setValue-52GetSet.drl", "UTF-8");
        KieHelper helper = new KieHelper();
        helper.addResource(resource);
        KieBase build = helper.build();
        KieSession kSession = build.newKieSession();
        UserDTO user = new UserDTO();
        kSession.insert(dataRow);
        kSession.insert(dataRow2);
        kSession.insert(user);
        kSession.fireAllRules();
        kSession.dispose();
        return user;
    }

    public UserDTO getUserWithPool_52GetSet(DataRow dataRow) {
        init_52GetSet();
        KieSession kSession = pool.newKieSession();
        UserDTO user = new UserDTO();
        kSession.insert(dataRow);
        kSession.insert(user);
        kSession.fireAllRules();
        kSession.dispose();
        return user;
    }

    public UserDTO getUserWithPool_2GetSet(DataRow dataRow) {
        init_2GetSet();
        KieSession kSession = pool.newKieSession();
        UserDTO user = new UserDTO();
        kSession.insert(dataRow);
        kSession.insert(user);
        kSession.fireAllRules();
        kSession.dispose();
        return user;
    }


    public void unitCase() {
        DataSource<UserDTO> persons = DataSource.create(new UserDTO("Mario", 42),
                new UserDTO("Marilena", 44),
                new UserDTO("Sofia", 4));

        Resource resource = ResourceFactory.newClassPathResource("rules/unit.drl", "UTF-8");
        KieHelper helper = new KieHelper();
        helper.addResource(resource);
        KieBase kbase = helper.build();
        RuleUnitExecutor executor = RuleUnitExecutor.create().bind(kbase);


        RuleUnit adultUnit = new ScopeUnit(persons, 18);
        executor.run(adultUnit);


    }
}
